<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Models\OrderComplain;
use App\Services\IAreaService;
use App\Services\IMasterService;
use App\Services\IOrderService;
use App\Services\ISessionService;
use Illuminate\Http\Request;

/**
 * 师傅相关
 * Class MasterController
 * @package App\Http\Controllers
 */
class MasterController extends BaseController
{

    /**
     * 首页
     * @param Request $request
     */
    public function index(Request $request, IMasterService $masterService)
    {
        $master = getCurrentUser();
        return view('master/index',['master'=>$master,
            'stars'=>$master->stat->stars
        ]);
    }

    /**
     * 个人资料
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail(IMasterService $masterService){
        $detail = $masterService->getDetailByUserid(getCurrentUserid());
        return view('master/detail',['master'=>getCurrentUser(),'detail'=>$detail]);
    }

    /**
     * 修改手机号码
     */
    public function changePhone(Request $request, IMasterService $masterService,ISessionService $sessionService)
    {
        return view('master/changePhone',['master'=>getCurrentUser(),'redirectUrl'=>$sessionService->getRedirectUrl('master/detail')]);
    }

    /**
     * 修改紧急联系手机号码
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function backupPhone(Request $request, IMasterService $masterService,ISessionService $sessionService){
        $master_id = getCurrentUserid();
        if ($request->isMethod('post')){
            $masterService->updateDetail($master_id,[
                'backup_phone'=>$request->input('backup_phone')
            ]);
            return redirect($sessionService->getRefererUrl());
        }

        $detail = $masterService->getDetailByUserid($master_id);
        return view('master/backupPhone',['detail'=>$detail]);
    }

    /**
     * 修改所在地区
     * @param Request $request
     * @param IMasterService $masterService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function changeArea(Request $request, IMasterService $masterService,ISessionService $sessionService,IAreaService $areaService){
        if ($request->isMethod('post')){
            $masterService->changeArea(getCurrentUserid(),$request->input('areaId'));
            return redirect($sessionService->getRefererUrl());
        }

        $parentsCouple = $areaService->getParentsCouple(getCurrentUser()->area_id);
        list($provinceId,$cityId,$districtId) = array_keys($parentsCouple);
        return view('master/changeArea',['user'=>getCurrentUser(),'provinceId'=>$provinceId,'cityId'=>$cityId,'districtId'=>$districtId]);
    }

    /**
     * 修改服务类型
     */
    public function changeServiceType(Request $request, IMasterService $masterService,ISessionService $sessionService)
    {
        if ($request->isMethod('post')){
            $masterService->changeServiceType(getCurrentUserid(),$request->input('service_type'));
            return redirect($sessionService->getRefererUrl());
        }

        return view('master/changeServiceType',['user'=>getCurrentUser(),
            'delivery'=>Params::$SERVICE_TYPE_DELIVERY,
            'carry'=>Params::$SERVICE_TYPE_CARRY,
            'install'=>Params::$SERVICE_TYPE_INSTALL,
            'repair'=>Params::$SERVICE_TYPE_REPAIR,
        ]);
    }

    /**
    * 修改服务区域
    */
    public function changeServiceAreas(Request $request, IMasterService $masterService,ISessionService $sessionService,IAreaService $areaService)
    {
        if ($request->isMethod('post')){
            $masterService->changeServiceAreaIds(getCurrentUserid(),$request->input('service_area_ids'));
            return redirect($sessionService->getRefererUrl());
        }

        $user = getCurrentUser();
        $city = $areaService->getParentArea($user->area_id);
        $districtCouple = $areaService->getDistrictCouple($city->id);

        return view('master/changeServiceAreas',['service_area_ids'=>$user->service_area_ids,'districtCouple'=>$districtCouple]);
    }

    /**
     * 修改附加服务
     */
    public function changeServiceExtra(Request $request, IMasterService $masterService,ISessionService $sessionService)
    {
        if ($request->isMethod('post')){
            $masterService->changeServiceExtra(getCurrentUserid(),$request->input('extra_finish_in'),$request->input('extra_nothing_fee'),
                $request->input('extra_repair_free'),$request->input('extra_floor_free'),$request->input('extra_carry_fee'),
                $request->input('extra_far_fee'),$request->input('extra_tmall_examine'),$request->input('extra_storage'),
                $request->input('extra_move_free'));
            return redirect($sessionService->getRefererUrl());
        }

        $detail = $masterService->getDetailByUserid(getCurrentUserid());
        return view('master/changeServiceExtra',['user'=>getCurrentUser(),
            'detail'=>$detail->toJson(),
            'extra_finish_in'=>Params::$SERVICE_EXTRA_FINISH_IN,
            'extra_nothing_fee'=>Params::$SERVICE_EXTRA_NOTHING_FEE,
            'extra_repair_free'=>Params::$SERVICE_EXTRA_REPAIR_FREE,
            'extra_floor_free'=>Params::$SERVICE_EXTRA_FLOOR_FREE,
            'extra_carry_fee'=>Params::$SERVICE_EXTRA_CARRY_FEE,
//            'extra_again_fee'=>Params::$SERVICE_EXTRA_AGAIN_FEE,
            'extra_far_fee'=>Params::$SERVICE_EXTRA_FAR_FEE,
        ]);
    }

    /**
     * 修改职业类型
     */
    public function changeJobType(Request $request, IMasterService $masterService,ISessionService $sessionService)
    {
        $master_id = getCurrentUserid();
        if ($request->isMethod('post')){
            $masterService->updateDetail($master_id,[
                'job_type'=>$request->input('job_type')
            ]);
            return redirect($sessionService->getRefererUrl());
        }

        $detail = $masterService->getDetailByUserid($master_id);
        return view('master/changeJobType',['job_type'=>$detail->job_type,
            'job_type_options'=>Params::$JOB_TYPE,
        ]);
    }

    /**
     * 修改服务时间
     */
    public function changeServicePeriod(Request $request, IMasterService $masterService,ISessionService $sessionService)
    {
        $master_id = getCurrentUserid();
        if ($request->isMethod('post')){
            $masterService->updateDetail($master_id,[
                'service_period'=>$request->input('service_period')
            ]);
            return redirect($sessionService->getRefererUrl());
        }

        $detail = $masterService->getDetailByUserid($master_id);
        return view('master/changeServicePeriod',['service_period'=>$detail->service_period,
            'weeks'=>Params::$WEEKS,
            'hours'=>Params::$HOURS,
        ]);
    }

    /**
     * 评价回复列表-待回复
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function evaluateWaiting(Request $request,IOrderService $orderService)
    {
        $listEvaluateWaiting = $orderService->listEvaluateWaiting(getCurrentUserid());
        return view('master/evaluateWaiting',['listEvaluateWaiting'=>$listEvaluateWaiting]);
    }

    /**
     * 评价回复列表-全部
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function evaluateAll(Request $request,IOrderService $orderService)
    {
        $listEvaluateAll = $orderService->listEvaluateAll(getCurrentUserid());
        return view('master/evaluateAll',['listEvaluateAll'=>$listEvaluateAll]);
    }

    /**
     * 投诉记录
     * @param Request $request
     * @param IOrderService $orderService
     */
    public function complain(Request $request,IOrderService $orderService)
    {
        list($waiting_complain_count,$handled_complain_count) = $orderService->getComplainCount(getCurrentUserid());
        return view('master/complain',['waiting_complain_count'=>$waiting_complain_count,'handled_complain_count'=>$handled_complain_count]);
    }

    /**
     * 投诉记录-核实中
     * @param Request $request
     * @param IOrderService $orderService
     */
    public function complainWaiting(Request $request,IOrderService $orderService)
    {
        $list = $orderService->getComplainWaitingList(getCurrentUserid());
        return view('master/complainWaiting',['list'=>$list]);
    }

    /**
     * 投诉记录-已处理
     * @param Request $request
     * @param IOrderService $orderService
     */
    public function complainHandled(Request $request,IOrderService $orderService)
    {
        $list = $orderService->getComplainHandledList(getCurrentUserid());
        return view('master/complainHandled',['list'=>$list]);
    }

    /**
     * 投诉记录-详情
     * @param Request $request
     * @param IOrderService $orderService
     */
    public function complainDetail(OrderComplain $complain,Request $request,IOrderService $orderService)
    {
        return view('master/complainDetail',['complain'=>$complain]);
    }

}
